package com.manelrodriguez.applications.ibex35.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.manelrodriguez.applications.ibex35.R;
import com.manelrodriguez.applications.ibex35.to.Empresa;

public class DescargaCotizacionesYahoo {
	SimpleDateFormat sdf,sdf2;
	
	
	public DescargaCotizacionesYahoo(){
		super();
	}

	public int descargar(URL param, ArrayList<Empresa> m_empresas, Map<String,Empresa> hash_empresas, Context context) {
		 try {
			 String porcentaje = null;
			 Document doc = Jsoup.connect(param.toString()).get();
			 Element tabla_general = (Element)doc.select("table.yfnc_tableout1").first().childNode(0).childNode(0).childNode(0).childNode(0);
			 Elements filas = tabla_general.getElementsByTag("tr");
			 for (Element fila : filas) {
				 if(fila.childNode(0).childNode(0) instanceof Element){
					 String nombreEmpresa = fila.childNode(0).childNode(0).childNode(0).childNode(0).toString();
					 String cotizacion = fila.childNode(2).childNode(0).childNode(0).toString();
					 String hora = fila.childNode(2).childNode(2).childNode(0).childNode(0).toString();
					 String variacion = fila.childNode(3).childNode(2).childNode(0).toString();
					 String volumen = fila.childNode(4).childNode(0).toString().replaceAll("\\.", "");
					 if(fila.childNode(3).childNodeSize()>3){
						 Element elemSigno = (Element)fila.childNode(3).childNode(0);
						 String signo = elemSigno.attr("alt");
						 if(signo.equals("Sube")){
							 variacion = "+" + variacion;
						 }else if(signo.equals("Baja")){
							 variacion = "-" + variacion;
						 }
						 if(fila.childNode(3).childNodeSize() > 4 && fila.childNode(3).childNode(4) instanceof Element){
							 porcentaje = fila.childNode(3).childNode(4).childNode(0).toString().trim().replaceAll("\\(", "").replaceAll("\\)", "");
						 }else if(fila.childNode(3).childNode(3) instanceof Element){
							 porcentaje = fila.childNode(3).childNode(3).childNode(0).toString().trim().replaceAll("\\(", "").replaceAll("\\)", "");
						 }
					 }else{
						 porcentaje = fila.childNode(3).childNode(2).childNode(0).toString().trim().replaceAll("\\(", "").replaceAll("\\)", "");
					 }
					 Empresa empresa = new Empresa();
					 empresa.setCompanyName(getIdEmpresaString(nombreEmpresa,context));
					 empresa.setStockSymbol(nombreEmpresa);
					 empresa.setLastTradeAmount(cotizacion);
					 empresa.setLastTradeDateTime(hora);
					 empresa.setStockChange(variacion);
  			    	 empresa.setOpenAmount("");
  			    	 empresa.setDayHigh("");
  			    	 empresa.setDayLow("");
  			    	 empresa.setStockVolume(volumen);
  			    	 empresa.setChangePercent(porcentaje);
  			    	 if(m_empresas!=null) m_empresas.add(empresa);
  			    	 if(hash_empresas!=null) hash_empresas.put(empresa.getStockSymbol(), empresa);
				 }
			 }
		 }catch (MalformedURLException e) {
			 return StocksURLS.ERROR_URL;
		 } catch (IOException e) {
			 return StocksURLS.ERROR_CONECTION;
		 }
		 return StocksURLS.RESULT_OK;
	}
	
	private String getIdEmpresaString(String companyName, Context context){
    	if(companyName.equals("^IBEX")) return context.getString(R.string.IBEX35);
    	else if(companyName.equals("ABE.MC")) return context.getString(R.string.ABE_MC);
		else if(companyName.equals("ABG-P.MC")) return context.getString(R.string.ABG_MC);
		else if(companyName.equals("ACS.MC")) return context.getString(R.string.ACS_MC);
		else if(companyName.equals("ACX.MC")) return context.getString(R.string.ACX_MC);
		else if(companyName.equals("AMS.MC")) return context.getString(R.string.AMS_MC);
		else if(companyName.equals("ANA.MC")) return context.getString(R.string.ANA_MC);
		else if(companyName.equals("BBVA.MC")) return context.getString(R.string.BBVA_MC);
		else if(companyName.equals("DIA.MC")) return context.getString(R.string.DIA_MC);
		else if(companyName.equals("BKT.MC")) return context.getString(R.string.BKT_MC);
		else if(companyName.equals("BME.MC")) return context.getString(R.string.BME_MC);
		else if(companyName.equals("CABK.MC")) return context.getString(R.string.CABK_MC);
		else if(companyName.equals("EBRO.MC")) return context.getString(R.string.EBRO_MC);
		else if(companyName.equals("ELE.MC")) return context.getString(R.string.ELE_MC);
		else if(companyName.equals("ENG.MC")) return context.getString(R.string.ENG_MC);
		else if(companyName.equals("FCC.MC")) return context.getString(R.string.FCC_MC);
		else if(companyName.equals("FER.MC")) return context.getString(R.string.FER_MC);
		else if(companyName.equals("GAS.MC")) return context.getString(R.string.GAS_MC);
		else if(companyName.equals("GRF.MC")) return context.getString(R.string.GRF_MC);
		else if(companyName.equals("IAG.MC")) return context.getString(R.string.IAG_MC);
		else if(companyName.equals("IBE.MC")) return context.getString(R.string.IBE_MC);
		else if(companyName.equals("IDR.MC")) return context.getString(R.string.IDR_MC);
		else if(companyName.equals("ITX.MC")) return context.getString(R.string.ITX_MC);
		else if(companyName.equals("MAP.MC")) return context.getString(R.string.MAP_MC);
		else if(companyName.equals("MTS.MC")) return context.getString(R.string.MTS_MC);
		else if(companyName.equals("OHL.MC")) return context.getString(R.string.OHL_MC);
		else if(companyName.equals("POP.MC")) return context.getString(R.string.POP_MC);
		else if(companyName.equals("REE.MC")) return context.getString(R.string.REE_MC);
		else if(companyName.equals("REP.MC")) return context.getString(R.string.REP_MC);
		else if(companyName.equals("SAB.MC")) return context.getString(R.string.SAB_MC);
		else if(companyName.equals("SAN.MC")) return context.getString(R.string.SAN_MC);
		else if(companyName.equals("SYV.MC")) return context.getString(R.string.SYV_MC);
		else if(companyName.equals("TEF.MC")) return context.getString(R.string.TEF_MC);
		else if(companyName.equals("TL5.MC")) return context.getString(R.string.TL5_MC);
		else if(companyName.equals("TRE.MC")) return context.getString(R.string.TRE_MC);
		else if(companyName.equals("VIS.MC")) return context.getString(R.string.VIS_MC);
		else return companyName;
    }
}
